package com.xindaoapp.happypet.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.AddressDetailInfo;
import com.xindaoapp.happypet.bean.BaseEntity;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.utils.ProgressHUD;

/**
 * 申请试用
 * 
 * @author xindaoapp
 * 
 */
public class ApplyForProbationActivity extends BaseActivity {

    private TextView mName, mNumber, mAddress;
    private RelativeLayout mRaddress;
    private EditText vMessage;

    private AddressDetailInfo addresss = null;
    private String addressID = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vMessage = (EditText) findViewById(R.id.content);
        mName = (TextView) findViewById(R.id.name);
        mNumber = (TextView) findViewById(R.id.phone_number);
        mAddress = (TextView) findViewById(R.id.address);
        mRaddress = (RelativeLayout) findViewById(R.id.r_address);
        mRaddress.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getBaseContext(), ConsigneeNewActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        getMoccaApi().getDefaultAddress(UserState.getUserUid(), new IRequest<AddressDetailInfo>() {

            @Override
            public void request(AddressDetailInfo t) {
                addresss = t;
                if (null != t) {
                    addressID = t.address_id;
                    mName.setText(t.username);
                    mNumber.setText(t.mobile);
                    mAddress.setText(t.address);
                } else {
                    mName.setText("未设置收货地址");
                    mNumber.setText("");
                    mAddress.setText("");
                }
            }

        });
    }

    @Override
    public void onResume() {
        super.onResume();
        onLoadDatas();
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.layout_applyforprobation;
    }

    @Override
    protected int getTopBarRightTextViewRes() {
        return R.string.submit;
    }

    @Override
    protected OnClickListener getRightViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null == addresss) {
                    Toast.makeText(getBaseContext(), "请先设置收货地址！", Toast.LENGTH_SHORT).show();
                    return;
                }
                final ProgressHUD progress =
                        ProgressHUD.show(ApplyForProbationActivity.this, "正在加载...", true, true,
                                null);

                getMoccaApi().activityApply(getIntent().getStringExtra(Constants.KEY_POST_TID),
                        addresss.username, addresss.address, addresss.mobile,
                        vMessage.getText().toString(), new IRequest<BaseEntity>() {

                            @Override
                            public void request(BaseEntity t) {
                                if (null != progress && progress.isShowing()) {
                                    progress.dismiss();
                                }
                                if (null != t) {
                                    Toast.makeText(getBaseContext(), t.getMsg(), Toast.LENGTH_SHORT)
                                            .show();
                                    if (t.getResult().equals("0")) {
                                        setResult(RESULT_OK);
                                        finish();
                                    }
                                }
                            }
                        });

            }
        };
    }

    @Override
    protected int getTopBarTitleRes() {
        return R.string.write_info;
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }



}
